from flask.blueprints import Blueprint 
from flask import jsonify,request
from utils.data import gain_data
from utils.comm import time_time
from database import db
import time

enter = Blueprint('enter',__name__)

@enter.route('/join',methods = ['POST','GET'])
def enter1():
    if request.method=='POST':
        gain = gain_data()
        sql = """
            insert into users(id,name,status,number) value(0,'%s',2,'%s')
        """%(gain['name'],gain['number'])
        db.insert(sql)
        stime = time.strftime('%Y-%m-%d %H-%M-%S',time.localtime())
        print(stime)
        sql1 = """
            select * from users where name='%s' and number='%s'
        """%(gain['name'],gain['number'])
        res1 = db.find(sql1)
        sql2 = """
            insert into record(start_time,user_id,number) value('%s',%d,'%s')
        """%(stime,int(res1['id']),gain['number'])
        db.insert(sql2)
        return jsonify({"code":200})
    else:
        sql = """
            select *from record
        """
        res = db.findall(sql)
        return jsonify({"code":200,'matter':res})

@enter.route('/go',methods = ['POST','GET'])
def go():
    if request.method == 'POST':
        pass
    else:
        id = request.args.get('id')
        sql = """
            select *from record where id=%d
        """%(int(id))
        res = db.find(sql)
        if res:
            etime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
            start = time_time(str(res['start_time']))
            # 更新结束停车时间
            end = time_time(etime)
            sql = """
                update record set end_time='%s' where id=%d
            """%(etime,int(id))
            db.insert(sql)
            # 计算时间差
            num = end-start

            # 查询用户是不是业主
            sql2 = """
                select *from users where id=%d
            """%(int(res['user_id']))
            res2 = db.find(sql2)

            # 查询每小时多少钱
            
        else:
            pass
